<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '秒杀添加' : '秒杀编辑'"
      :visible.sync="obj.isTrue"
      width="50%"
      @close="cancel"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="活动名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="活动期限">
          <div class="block">
            <el-date-picker
              v-model="value"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="changeTime"
              value-format="timestamp"
            >
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="一级菜单">
          <el-select
            v-model="form.first_cateid"
            placeholder="请选择"
            @change="changeCate"
          >
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级菜单">
          <el-select
            v-model="form.second_cateid"
            placeholder="请选择"
            @change="SecondCate"
          >
            <el-option
              v-for="item in secondCate"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择商品">
          <el-select v-model="form.goodsid" placeholder="请选择">
            <el-option
              v-for="item in goods"
              :key="item.id"
              :label="item.goodsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add" v-if="obj.isAdd"
          >添 加</el-button
        >
        <el-button type="primary" @click="edit" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {
  cateList,
  goodsList,
  seckAdd,
  seckInfo,
  seckEdit,
} from "../../../request/api";
import { successAlert, warningAlert } from "../../../utils/alert";
export default {
  props: ["obj"],
  components: {},
  data() {
    return {
      form: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      },
      //用来保存开始和结束时间
      value: [],
      //用来保存二级分类
      secondCate: [],
      //用来保存商品
      goods: [],
    };
  },
  computed: {
    ...mapGetters({
      cateList: "cate/list",
      goodsList: "goods/list",

      seckillList: "seckill/list",
    }),
  },
  methods: {
    ...mapActions({
      asyncCateList: "cate/asyncChangeList",

      asyncSeckillList: "seckill/asyncChangeList",
    }),
    changeTime() {
      //1.将时间戳转换为 2020-08-09 06:05:04 格式
      // var date = new Date(this.form.begintime)
      // var Y = date.getFullYear() + '-'
      // var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
      // var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
      // var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
      // var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
      // var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
      // let time= Y + M + D + h + m + s
      // console.log(time);

      //2.直接使用element ui 转换时间格式  对应位置加上value-format="yyyy-MM-dd HH:mm:ss"
      this.form.begintime = this.value[0];
      this.form.endtime = this.value[1];
    },
    changeCate() {
      cateList({ pid: this.form.first_cateid }).then((res) => {
        //每次选择之前 先清空上次选择的分类
        this.form.second_cateid = "";
        this.secondCate = res.data.list;
      });
    },
    SecondCate() {
      goodsList({
        fid: this.form.first_cateid,
        sid: this.form.second_cateid,
      }).then((res) => {
        //每次选择之前 先清空上次选择的分类
        this.form.goodsid = "";
        this.goods = res.data.list;
      });
    },
    cancel() {
      this.obj.isTrue = false;
      (this.form = {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      }),
        (this.value = []);
      this.secondCate = [];
    },
    add() {
      console.log(this.form);
      seckAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.asyncSeckillList();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    getInfo(id) {
      this.obj.isTrue = true;
      this.obj.isAdd = false;
      seckInfo({ id: id }).then((res) => {
        this.form = {
          ...res.data.list,
          id: id,
        };
        //根据一级菜单的pid渲染二级菜单
        cateList({ pid: this.form.first_cateid }).then((res) => {
          console.log(res);
          this.secondCate = res.data.list;
        });
        //渲染商品列表
        goodsList({
          fid: this.form.first_cateid,
          sid: this.form.second_cateid,
        }).then((res) => {
          this.goods = res.data.list;
        });
        //日期回显到编辑页面
        let time1 = new Date(Number(this.form.begintime));
        let time2 = new Date(Number(this.form.endtime));
        this.value = [time1, time2];
      });
    },
    edit() {
      seckEdit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.asyncSeckillList();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
  },
  mounted() {
    this.asyncCateList();
  },
};
</script>
<style scoped>
</style>